package com.huadao.cart_ccs.greendao;

import com.huadao.cart_ccs.common.Constants;
import com.huadao.cart_ccs.common.FileUtils;
import com.huadao.cart_ccs.data.bo.LogInfo;
import com.huadao.cart_ccs.data.bo.Protocol;
import com.huadao.cart_ccs.greendao.entity.CO2;
import com.huadao.cart_ccs.greendao.entity.ExceptionInfo;
import com.huadao.cart_ccs.greendao.entity.Injection;
import com.huadao.cart_ccs.greendao.entity.O2;
import com.huadao.cart_ccs.greendao.entity.Sample;
import com.huadao.cart_ccs.greendao.entity.Temperature;
import com.huadao.cart_ccs.greendao.gen.DaoSession;
import com.huadao.cart_ccs.main.CartApplication;

import org.greenrobot.greendao.async.AsyncSession;
import org.greenrobot.greendao.identityscope.IdentityScopeType;

import java.io.File;

public class GreenDaoManager {

    private static GreenDaoManager mInstance;
    private MyDaoMaster mDaoMaster;
    private DaoSession mNoCacheDaoSession;
    private DaoSession mDaoSession;
    private static AsyncSession asyncNoCacheSession;
    private static AsyncSession asyncSession;

    private GreenDaoManager() {
        File path = new File(FileUtils.getFilePath(CartApplication.getInstance()) + Constants.dbDir, Constants.dbName);
        path.getParentFile().mkdirs();
        MyDaoMaster.DevOpenHelper devOpenHelper = new MyDaoMaster.DevOpenHelper(CartApplication.getInstance(), path
                .getAbsolutePath(), null);
        mDaoMaster = new MyDaoMaster(devOpenHelper.getWritableDatabase());
        mNoCacheDaoSession = mDaoMaster.newSession(IdentityScopeType.None);
        mDaoSession = mDaoMaster.newSession();
        asyncNoCacheSession = mNoCacheDaoSession.startAsyncSession();
        asyncSession = mDaoSession.startAsyncSession();
    }

    public static GreenDaoManager getInstance() {
        if (mInstance == null) {
            synchronized (GreenDaoManager.class) {
                mInstance = new GreenDaoManager();
            }
        }
        return mInstance;
    }

    public DaoSession getNoCacheSession() {
        return mNoCacheDaoSession;
    }

    public DaoSession getSession() {
        return mDaoSession;
    }

    public AsyncSession getAsyncNoCacheSession() {
        return asyncNoCacheSession;
    }


    //删除数据库所有数据
    public void deleteAllData() {
        mNoCacheDaoSession.deleteAll(LogInfo.class);
        // mDaoSession.deleteAll(Project.class);
        // mNoCacheDaoSession.deleteAll(ExperimentStage.class);
        mNoCacheDaoSession.deleteAll(ExceptionInfo.class);
        mNoCacheDaoSession.deleteAll(Protocol.class);
        mNoCacheDaoSession.deleteAll(CO2.class);
        mNoCacheDaoSession.deleteAll(O2.class);
        mNoCacheDaoSession.deleteAll(Temperature.class);
        mNoCacheDaoSession.deleteAll(Injection.class);
        mNoCacheDaoSession.deleteAll(Sample.class);
    }



    //清除数据库所有缓存
    public void clearAllCache() {
        mNoCacheDaoSession.clear();
    }
}
